import { Component, Input, ChangeDetectorRef } from "@angular/core";
import { SliderLevelModel } from "../../../../../../../shared/components/slider/sliderLevel.model";
import { BaseCriteriaTemplateComponent } from "../baseType.component";
import { ReportingService } from "../../../../../reporting.service";
import { Subscription } from "rxjs";
import * as _ from 'lodash';
import { ICriteriaTemplate, TemplateType } from "../../models/criteriaTemplate.interface";
import { ICriteriaOutput } from "../../models/save/criteriaOutput.interface";
import { SliderOutput } from "../../models/save/criteriaOutput.model";

@Component({
    selector: 'type25',
    templateUrl: 'type25.component.html',
    styleUrls: ['../../templateCriteria.component.scss'],
    providers: [{ provide: BaseCriteriaTemplateComponent, useExisting: Type25Component }]
})
export class Type25Component extends BaseCriteriaTemplateComponent implements ICriteriaTemplate {
    private sub: Subscription;
    public selectedDesc: string;
    public selectedId: number;
    public levels: SliderLevelModel[]= [
        new SliderLevelModel(0, 'Off', 'No Minimum threshold selected'),
        new SliderLevelModel(5, 'Severe', 'Companies with Severe Risk are restricted'),
        new SliderLevelModel(4, 'High', 'Companies with Severe and High Risk are restricted'),
        new SliderLevelModel(3, 'Medium', 'Companies with Severe, High and Medium Risk are restricted'),
        new SliderLevelModel(2, 'Low', 'Companies with Severe, High, Medium and Low Risk are restricted'),
        new SliderLevelModel(1, 'Negligible', 'Companies with Severe, High, Medium, Low and Negligible Risk are restricted')];

    public type: TemplateType = TemplateType.Slider;

    @Input() set answerCategories(value: any[]) {

        let groupedCategories = _(value).groupBy('score').map((items, key) => {
            return { key: key, items: items };
        }).value();

        _.sortBy(groupedCategories, item => {
            if (item.key > -1)
                return groupedCategories.length - parseInt(item.key);
            else
                return -1;
        });
    }

    constructor(
        private toolsService: ReportingService,
        private cdRef: ChangeDetectorRef) {
        super(toolsService, cdRef);
    }

    ngOnInit() {
        this.loadScoreData('type25IsScreenableCriteriaItems')
        this.sub = this.load.subscribe(data => {
            this.selected = data || 0;
        });
    }

    ngOnDestroy() {
        this.sub.unsubscribe();
    }

    public set selected(value: number) {
        if (value !== undefined) {
            this.selectedId = value + 1;
            this.selectedDesc = this.levels.find(x => x.id == this.selectedId).desc;
        }
    }

    public sliderChanged(model) {
        this.selectedId = model.id;
        this.selectedDesc = model.desc;
        BaseCriteriaTemplateComponent.changed.emit(model.id != 0);
    }

    public save(): ICriteriaOutput {
        let model: SliderOutput = new SliderOutput(this.selectedId - 1, undefined);
        model.setBaseClass(this.templateCriteria);
        return model;
    }
}